package com.example.demo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

public class GameFrame extends JFrame {
    //双缓冲图片
    private Image offScreenImge = null;
    //攻击图片
    private Image attack = Toolkit.getDefaultToolkit().getImage("img/attack.png");
    //游戏背景
    Background background = new Background(this);
    //玩家
    Champion player = new ChampionDaji(this);
    //双方小兵
    MinionBlue mb = new MinionBlue(this);
    MinionRed mr = new MinionRed(this);
    //防御塔
    Turret turret = new Turret(this);
    //游戏元素列表
    ArrayList<GameObject> objList = new ArrayList<GameObject>();
    ArrayList<GameObject> redList = new ArrayList<GameObject>();//红色方
    ArrayList<GameObject> blueList = new ArrayList<GameObject>();//蓝色方
    ArrayList<GameObject> removeList = new ArrayList<GameObject>();//存放将要删除的元素

    public void launch(){

        setSize(1400,700);
        setLocationRelativeTo(null);
        setTitle("小窗口");
        setDefaultCloseOperation(3);
        setVisible(true);
        //添加键盘监视器
        this.addKeyListener(new GameFrame.KeyMonitor());
        //添加游戏元素
        objList.add(background);
        objList.add(player);
        objList.addAll(turret.turretList);
        for (int i = 0;i < 4;i++){
            blueList.add(turret.turretList.get(i));
        }
        for (int i = 4;i < 8; i++){
            redList.add(turret.turretList.get(i));
        }

        //攻击按钮
        JButton button = new JButton();
        button.setSize(130,132);
        button.setLocation(1150,430);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //按键事件
                player.attack(redList);
            }
        });
        this.add(button);
        player.addButton();

        while (true){
            mb.createMinion(this,blueList);
            mr.createMinion(this,redList);
            repaint();
            try{
                Thread.sleep(25);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    public void paint(Graphics g){
        //System.out.println(player.getX()+""+player.getY());
        if (offScreenImge == null){
            offScreenImge = this.createImage(5984,4452);
        }
        Graphics gImage = offScreenImge.getGraphics();
        for (int i = 0 ; i < objList.size() ; i++){
            objList.get(i).paintSelf(gImage);
        }
        //绘制攻击图片
        gImage.drawImage(attack,player.getX()+500,player.getY()+100,null);
        objList.removeAll(removeList);

        g.drawImage(offScreenImge,-player.getX()+700,-player.getY()+350,null);
        this.requestFocus();
    }

    public static void main(String[] args) {
        GameFrame gameFrame = new GameFrame();
        gameFrame.launch();
    }

    //键盘事件
    class KeyMonitor extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent e){
            int key = e.getKeyCode();
            player.KeyPressed(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            player.KeyReleased(e);
        }
    }
}
